using Newtonsoft.Json.Linq;
using System.IO;
using UnityEngine;

namespace FrameWork
{
    public class FileModule : BaseModule
    {
        public override void Init()
        {
            

        }

        public override void ShutDown()
        {
            
        }

        public override void Update(float deltaTime, float realDeltaTime)
        {
            
        }

        /// <summary>
        /// 从某个路径读取字符串
        /// </summary>
        public string ReadFromPath(string path)
        {
            if (!ExistFile(path))
                return string.Empty;

            StreamReader sr = new StreamReader(path);
            string storageStr = sr.ReadToEnd();
            sr.Dispose();
            return storageStr;
        }

        public void WriteToPath(string content, string path)
        {
            if (!ExistFile(path))
                CreateFile(path);

            //StreamWriter sw = new StreamWriter(path);
            //sw.WriteLine(content);
            File.WriteAllText(path, content);
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        private void CreateFile(string path)
        {
            if (ExistFile(path))
            {
                Debug.LogWarning("文件已经存在");
                return;
            }

            File.Create(path).Dispose();
        }

        /// <summary>
        /// 检查文件是否存档
        /// </summary>
        public bool ExistFile(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// 获取某个节点值
        /// </summary>
        public string JsonSelectNode(JToken json,string ReName)
        {
            var node = json.SelectToken(ReName);
            string result = node.Value<object>().ToString();
            return result;
        }
    }
}

